package store

import (
	"golang.org/x/net/context"
	mgo "gopkg.in/mgo.v2"
)

var (
	mongodb_url = "localhost:9005"
	gSession    *mgo.Session
)

func SetMongoDBURL(url string) {
	mongodb_url = url
}

func GetDBURL() string {
	return mongodb_url
}

func GetCollect(c context.Context, collectName string) *mgo.Collection {
	session := c.Value("db").(*mgo.Session)
	return session.DB("tasks").C(collectName)
}

func CopySession() (session *mgo.Session, err error) {
	if gSession == nil {

		gSession, err = mgo.Dial(GetDBURL())
		if err != nil {
			//utils.PrintError([]string{"数据库连接信息: ", "错误信息"},[]string{GetDBURL(), err.Error()})
			return
		}
	}

	session = gSession.Copy()
	return
}
